package com.sumainfor.wx.system.epc.service;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.wx.system.epc.dao.RepairMapper;
import com.sumainfor.wx.system.feign.MarketFeign.MarketFeign;
import com.sumainfor.wx.system.feign.TenantFeign.TenantFeign;
import com.sumainfor.wx.system.feign.ZuulFeign.ZuulFeign;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 维修工单池
 * @Auther: zhlu
 * @Date: 2019/6/12 11:27
 * @Description:
 */
@Service
@Log4j2
public class RepairService {

    @Autowired
    RepairMapper RepairMapper;

    @Autowired
    ZuulFeign ZuulFeign;

    @Autowired
    MarketFeign MarketFeign;

    @Autowired
    TenantFeign TenantFeign;

    /**
     * 功能描述: 
     * 获取当前服务中心维修工单池列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/12 12:05
     */
    public List<Map<String,Object>> getRepairList(Map<String,Object>params){
        //获取服务中心编号
        JsonResult jsonResult=ZuulFeign.getCenterServiceInfo(ContextUtils.getUserId());
        if(jsonResult.isSuccess()==true){
            Integer CenterServiceInfo=(Integer)jsonResult.getResult();
            log.info("服务中心编号-----"+CenterServiceInfo);
            params.put("MiId",CenterServiceInfo);
        }
        //获取维修工单池
        List<Map<String,Object>>getRepairList=RepairMapper.getRepairList(params);
        if(getRepairList.size()>0){
            //获取房源资料
            JsonResult jsonResult1=MarketFeign.getAllHousingConLists();
            log.info("jsonResult1----"+jsonResult1);
            List<Map<String,Object>>AllHousingConLists=(List<Map<String,Object>>)jsonResult1.getResult();
            getRepairList= ListMapMergeUtil.ListMergeUtils(getRepairList,AllHousingConLists,"HouCon");

            //获取报修人
            for(Map<String,Object> map:getRepairList){
                Integer RepType=Integer.valueOf(map.get("RepType").toString());
                List<String>ids=new ArrayList<String>();
                ids.add(map.get("RepairsId").toString());
                switch (RepType){
                    case 1://报修人为普通用户
                        //获取租客资料
                        List<Map<String,Object>> TenantInfoByIdList=TenantFeign.selectTenantInfoByIdList(ids);
                        if(TenantInfoByIdList.size()>0){
//                            map.putAll(TenantInfoByIdList.get(0));
                            map.put("RepName",TenantInfoByIdList.get(0).get("Realname"));
                        }else{
                            map.put("RepName",null);
                        }
                        break;
                    case 2://管家报修
                        JsonResult jsonResult3=ZuulFeign.getUser(ids);
                        List<Map<String,Object>>UserList=(List<Map<String,Object>>)jsonResult3.getResult();
                        if(UserList.size()>0){
//                            map.putAll(UserList.get(0));
                            map.put("RepName",UserList.get(0).get("username"));
                        }else {
                            map.put("RepName",null);
                        }
                        break;
                }
            }
        }
        return getRepairList;
    }

    /**
     * 功能描述: 
     * 获取当前服务中心维修工单池列表总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/12 12:05
     */
    public Integer getRepairListCount(Map<String,Object>params){
        //获取服务中心编号
        JsonResult jsonResult=ZuulFeign.getCenterServiceInfo(ContextUtils.getUserId());
        if(jsonResult.isSuccess()==true){
            Integer CenterServiceInfo=(Integer)jsonResult.getResult();
            log.info("CenterServiceInfo-----"+CenterServiceInfo);
            params.put("MiId",CenterServiceInfo);
        }
        return RepairMapper.getRepairListCount(params);
    }
}
